package com.db.stockexchange.domain;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.db.stockexchange.domain.order.Order;


public class Exchange {
	private Set<String> supportedSymbols;
	private Map<String, OrderBook> orderBookBySymbol = new HashMap<>();
	
	public Exchange(List<String> symbols) {
		supportedSymbols = new HashSet<>();
		for (String symbol : symbols)
			supportedSymbols.add(symbol);
		
		for (String symbol : supportedSymbols) {
			orderBookBySymbol.put(symbol, new OrderBook());
		}
	}
	
	public List<ExecutionReport> placeOrder(Order order) {
		List<ExecutionReport> reports = new LinkedList<>();
		OrderBook book = orderBookBySymbol.get(order.getSymbol());
		
		if (book == null) {
			order.reject();
			reports.add(new ExecutionReport(order, "unknown symbol"));
			return reports;
		}
		
		reports.add(new ExecutionReport(order));
		reports.addAll(book.placeOrder(order));
		return reports;
	}

}
